<?php
namespace App\Http\Middleware;
use Closure;
use Hlcreation\Services\CacheServices;


class CheckAdminLogin
{
    public $authUserInfo;
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        //判断是否登录
        $header = $request->header();
        if (!isset($header['authori-zation']) || (isset($header['authori-zation']) && empty($header['authori-zation'][0]))) {
            return response()->json([ 'code' => 410002, 'msg' => '请登录']);
        }
        $token = $header['authori-zation'][0];
        if (!$res_cache = CacheServices::get($token)) {
            return response()->json([ 'code' => 410002, 'msg' => '请正常登录']);
        }
        CacheServices::setTarget($token);
        $userInfo = json_decode($res_cache, true);
       
        if (empty($userInfo['user_info'])) {
            return response()->json([ 'code' => 410002, 'msg' => '请正常登录']);
        }
        $request->attributes->add(['userinfo' => $userInfo, 'token'=>$token]);
       
        return $next($request);
    }





}
